package com.kxmall.web.controller.decorate;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.decorate.domain.bo.KxPageTemplateBo;
import com.kxmall.decorate.domain.vo.KxPageTemplateVo;
import com.kxmall.web.controller.decorate.service.IKxPageTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 首页页面模板
 *
 * @author kxmall
 * @date 2023-11-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/decorate/pageTemplate")
public class KxPageTemplateController extends BaseController {

    private final IKxPageTemplateService iKxPageTemplateService;

    /**
     * 查询首页页面模板列表
     */
    @SaCheckPermission("decorate:pageTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<KxPageTemplateVo> list(KxPageTemplateBo bo, PageQuery pageQuery) {
        return iKxPageTemplateService.queryPageList(bo, pageQuery);
    }



    /**
     * 获取首页页面模板详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<KxPageTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(iKxPageTemplateService.queryById(id));
    }

    /**
     * 新增首页页面模板
     */
    @SaCheckPermission("decorate:pageTemplate:add")
    @Log(title = "首页页面模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KxPageTemplateBo bo) {
        return toAjax(iKxPageTemplateService.insertByBo(bo));
    }

    /**
     * 修改首页页面模板
     */
    @SaCheckPermission("decorate:pageTemplate:edit")
    @Log(title = "首页页面模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KxPageTemplateBo bo) {
        return toAjax(iKxPageTemplateService.updateByBo(bo));
    }

    /**
     * 删除首页页面模板
     *
     * @param ids 主键串
     */
    @SaCheckPermission("decorate:pageTemplate:remove")
    @Log(title = "首页页面模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iKxPageTemplateService.deleteWithValidByIds(Arrays.asList(ids), true));
    }



    /**
     * 新增首页页面模板
     */
    @SaCheckPermission("decorate:pageTemplate:edit")
    @RepeatSubmit()
    @PostMapping("/homeTemplateLaunch")
    public R<Void> homeTemplateLaunch(@RequestBody KxPageTemplateBo bo) {
        return toAjax(iKxPageTemplateService.homeTemplateLaunch(bo));
    }
}
